<?php
	require('health.doc.php');
	//我的健康数据
	$app->get(
		'/health/info',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
           if($data && isset($data['memberid']) && isset($data['from']) && isset($data['to'])){
               $sql = "select count(*) as sum from tb_health where member_id=?";
               $ht = $db2->prepare($sql);
               $ht->execute(array($data['memberid']));
               $tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
               $echo['count'] = $tmp[0]['sum'];
               
                $sql = "select a.*,b.sex from tb_health as a LEFT JOIN tb_member as b on a.member_id=b.tbid ".
                "where a.member_id=? order by dt desc limit ?,?";
                $ht = $db2->prepare($sql);
                $ht->execute(array($data['memberid'], $data['from'], $data['to']));
                $health = $ht->fetchAll(PDO::FETCH_ASSOC);
               
				$echo['data'] = $health;
				
				
            }else{
                $echo['error'] = "数据缺失，请检查";
            }
			echo json_encode($echo);
		}
	);
    //我的基本信息
    $app->get(
		'/health/baseinfo',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
            if($data && isset($data['memberid']) && isset($data['from']) && isset($data['to'])){
				$sql = "select count(*) as sum from tb_health_baseinfo where member_id=?";
                $ht = $db2->prepare($sql);
                $ht->execute(array($data['memberid']));
                $tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
                $echo['count'] = $tmp[0]['sum'];
                
                $sql = "select * from tb_health_baseinfo where member_id=? order by dt desc limit ?,?";
                $ht = $db2->prepare($sql);
                $ht->execute(array($data['memberid'], $data['from'], $data['to']));
                $health = $ht->fetchAll(PDO::FETCH_ASSOC);
               
				$echo['data'] = $health;
            }else{
                $echo['error'] = "数据缺失，请检查";
            }
			echo json_encode($echo);
		}
	);
	
	//我的健康数据七天数据
	$app->get(
	    '/health/info/7day',
	    function() use($app, $db2){
	        $data = $app->request()->get();
	        $echo['error'] = "";
	        if($data && isset($data['memberid']) && isset($data['column']) && isset($data['type'])){
	            for($i=6;$i>=0;$i--){
	                if($data['type'] == 1){
	                   $echo['data'][]=$db2->select(3, 'tb_health', $data['column'], array('member_id' => $data['memberid'],'to_days(now())-to_days(dt)'=>$i));
	                }else if($data['type'] == 2){
	                    $tmp = strtotime( '-'.($i+1).' week') + 3600*24;
	                    $tmp1 = $tmp + 6*3600*24;
	                    //echo date('Y-m-d', $tmp)." ".date('Y-m-d', $tmp1)."\n";
	                    $begin_dt = date('Y-m-d', $tmp)." 00:00";
	                    $end_dt = date('Y-m-d', $tmp1)." 23:59";
	                    
	                    
	                    $sql = "select avg(".$data['column'].") as co from tb_health where member_id = ? and dt >= ? and dt <= ?";
	                    $ht = $db2->prepare($sql);
	                    $ht->execute(array($data['memberid'], $begin_dt, $end_dt));
	                    $health = $ht->fetchAll(PDO::FETCH_ASSOC);
	                    
	                    $echo['data'][] = (int)$health[0]['co'];
	                }else if($data['type'] == 3){
	                    $tmp = strtotime( '-'.($i).' month');
	                    $tmp1 = strtotime( '-'.($i-1).' month');
	                    //echo date('Y-m-d', $tmp)." ".date('Y-m-d', $tmp1)."\n";
	                    $begin_dt = date('Y-m', $tmp)."-01 00:00";
	                    $end_dt = date('Y-m', $tmp1)."-01 00:00";
	                    
	                    $sql = "select avg(".$data['column'].") as co from tb_health where member_id = ? and dt >= ? and dt < ?";
	                    $ht = $db2->prepare($sql);
	                    $ht->execute(array($data['memberid'], $begin_dt, $end_dt));
	                    $health = $ht->fetchAll(PDO::FETCH_ASSOC);
	                    
	                    $echo['data'][] = (int)$health[0]['co'];
	                }
	                
	                
	            }
	        }else{
	            $echo['error'] = "数据缺失，请检查";
	        }
	        echo json_encode($echo);
	    }
	);
    //我的基本信息七天数据
    $app->get(
		'/health/baseinfo/7day',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
            if($data && isset($data['memberid']) && isset($data['column']) && isset($data['type'])){
                for($i=6;$i>=0;$i--){
	                if($data['type'] == 1){
	                   $echo['data'][]=$db2->select(3, 'tb_health_baseinfo', $data['column'], array('member_id' => $data['memberid'],'to_days(now())-to_days(dt)'=>$i));
	                }else if($data['type'] == 2){
	                    $tmp = strtotime( '-'.($i+1).' week') + 3600*24;
	                    $tmp1 = $tmp + 6*3600*24;
	                    //echo date('Y-m-d', $tmp)." ".date('Y-m-d', $tmp1)."\n";
	                    $begin_dt = date('Y-m-d', $tmp)." 00:00";
	                    $end_dt = date('Y-m-d', $tmp1)." 23:59";
	                    
	                    $sql = "select avg(".$data['column'].") as co from tb_health_baseinfo where member_id = ? and dt >= ? and dt <= ?";
	                    $ht = $db2->prepare($sql);
	                    $ht->execute(array($data['memberid'], $begin_dt, $end_dt));
	                    $health = $ht->fetchAll(PDO::FETCH_ASSOC);
	                    
	                    $echo['data'][] = (int)$health[0]['co'];
	                }else if($data['type'] == 3){
	                    $tmp = strtotime( '-'.($i).' month');
	                    $tmp1 = strtotime( '-'.($i-1).' month');
	                    //echo date('Y-m-d', $tmp)." ".date('Y-m-d', $tmp1)."\n";
	                    $begin_dt = date('Y-m', $tmp)."-01 00:00";
	                    $end_dt = date('Y-m', $tmp1)."-01 00:00";
	                    
	                    $sql = "select avg(".$data['column'].") as co from tb_health_baseinfo where member_id = ? and dt >= ? and dt < ?";
	                    $ht = $db2->prepare($sql);
	                    $ht->execute(array($data['memberid'], $begin_dt, $end_dt));
	                    $health = $ht->fetchAll(PDO::FETCH_ASSOC);
	                    
	                    $echo['data'][] = (int)$health[0]['co'];
	                }
	                
	                
	            }
            }else{
                $echo['error'] = "数据缺失，请检查";
            }
			echo json_encode($echo);
		}
	);
    //编辑我的基本信息
    $app->post(
        '/health/baseinfo/edit',
        function() use($app, $db2){
            $data = $app->request()->post();
            $echo['error'] = "";
            if($data && isset($data['memberid'])){
				if(!empty($data['healthid'])){
					$baseinfo = $db2->select(1, 'tb_health_baseinfo', '*', array('member_id' => $data['memberid'],'tbid'=>$data['healthid']));
					if(date('Y-m-d',strtotime($baseinfo['dt']))!=date('Y-m-d')){
						$echo['error'] = "该条数据不允许编辑";
						 echo json_encode($echo);exit;
					}
				}
				
				if(isset($data['weight'])){
					$set['weight'] = $data['weight'];
				}
				if(isset($data['fat_rate'])){
					$set['fat_rate'] = $data['fat_rate'];
				}
				if(isset($data['bust'])){
					$set['bust'] = $data['bust'];
				}
				if(isset($data['waist'])){
					$set['waist'] = $data['waist'];
				}
				if(isset($data['hipline'])){
					$set['hipline'] = $data['hipline'];
				}
				if(isset($data['thigh'])){
					$set['thigh'] = $data['thigh'];
				}
				if(isset($data['shoulder_width'])){
					$set['shoulder_width'] = $data['shoulder_width'];
				}
				if(isset($data['sebum'])){
					$set['sebum'] = $data['sebum'];
				}
				if(isset($data['arm'])){
				    $set['arm'] = $data['arm'];
				}

				if(!empty($data['healthid'])){
					$db2->update('tb_health_baseinfo', $set, array('member_id' => $data['memberid'],'tbid'=>$data['healthid']));
				}else{
					if($db2->select(2, 'tb_health_baseinfo', '*', array('member_id' => $data['memberid'],'#to_days(dt)'=>'to_days(now())'))==0){
						$set['member_id'] = $data['memberid'];
						$set['#dt']='now()';
						$db2->insert('tb_health_baseinfo', $set);
					}else{
               			$echo['error'] = "今天已提交过数据，不允许重复提交";
					}
				}
            }else{
                $echo['error'] = "数据缺失，请检查";
            }
            echo json_encode($echo);
        }
    );
    //编辑我的健康信息
    $app->post(
        '/health/info/edit',
        function() use($app, $db2){
            $data = $app->request()->post();
            $echo['error'] = "";
            if($data && isset($data['memberid'])){
				if(!empty($data['healthid'])){
					$baseinfo = $db2->select(1, 'tb_health', '*', array('member_id' => $data['memberid'],'tbid'=>$data['healthid']));
					if(date('Y-m-d',strtotime($baseinfo['dt']))!=date('Y-m-d')){
						$echo['error'] = "该条数据不允许编辑";
						 echo json_encode($echo);exit;
					}
				}
				if(isset($data['age'])){
					$set['age'] = $data['age'];
				}
				if(isset($data['height'])){
					$set['height'] = $data['height'];
				}

				if(!empty($data['healthid'])){
					$db2->update('tb_health', $set, array('member_id' => $data['memberid'],'tbid'=>$data['healthid']));
				}else{
					if($db2->select(2, 'tb_health', '*', array('member_id' => $data['memberid'],'#to_days(dt)'=>'to_days(now())'))==0){
						$set['member_id'] = $data['memberid'];
						$set['#dt']='now()';
						$db2->insert('tb_health', $set);
					}else{
               			$echo['error'] = "今天已提交过数据，不允许重复提交";
					}
				}
            }else{
                $echo['error'] = "数据缺失，请检查";
            }
            echo json_encode($echo);
        }
    );
?>
